#include "Resource.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "WorldPacket.h"
#include "ScriptMgr.h"
#include "WSSocket.h"
#include "Opcodes.h"
#include "Users.h"
#include "UserManager.h"
#include "MD5.h"
#include "Towns.h"
#include "OpcodesEx.h"
#include "Mails.h"
#include "MailManager.h"
#include "ChannelManager.h"
#include "Message.h"
#include "WSSocketManager.h"
#include "ResourceEventEnum.h"
#include "NoticeManager.h"
#include "GameThreadLogic.h"
#include "GatewayOpcodes.h"
#include "json/json.h"
#include "StringScreen.h"
#include "ProtocolDealEnums.h"
#include "MJScriptWorldProcess.h"
#include "GameDefine.h"
#include LOG_MGR_PATH
#include LOGIC_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include XMLDATA_MGR_PATH
#include "GameGoodsManager.h"
#include "GlobalCommonFunctons.h"
#include "GameDataEnum.h"
#include "subgames/Morra.h"
#include "CenterBankManager.h"
// #include "GoldCoronal.h"
#include "DataTransferManager.h"
#include "VipCardManager.h"
#include "PubDataManager.h"
#include "Tools.h"
// #include "CenterBattleManager.h"
// #include "CenterBattleLogManager.h"
// #include "CenterFriendManager.h"
#include "CenterHappyFarmManager.h"
#include "CRUsersManager.h"
#include "showip.h"
#include "AIInterface.h"


using namespace AIScript;

//CMSS_MJ_OutCard = 0x2506,		//出牌 对玩家自己
bool  script_world_process_MJ_CastCard(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr chr = socket->getCurrentCharacter();
	if (chr.isNull())
	{
		return true;
	}

	AICreatureBaseInterface* aichrInter = TO_CREATURE_BASE_INTERFACE(chr->getAIInterface());
	if (!aichrInter)
	{
		return true;
	}

	ChannelPtr chnPtr = aichrInter->GetChannelPtr();

	if (chnPtr.isNull())
	{
		return true;
	}

	uint8 ui8OutCardData = INVALID_uint8;

	packet >> ui8OutCardData;

	AIMaJiangChannelBaseInterface* aichanInter = TO_MJCHANNEL_INTERFACE(chnPtr->getAIInterface());
	if (aichanInter)
	{
		aichanInter->UserOutCard(chr, ui8OutCardData);
	}
	return true;
}

//CMSS_MJ_Operate = 0x2509,		//玩家动作
bool  script_world_process_MJ_Operate(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr chr = socket->getCurrentCharacter();
	if (chr.isNull())
	{
		return true;
	}

	AICreatureBaseInterface* aichrInter = TO_CREATURE_BASE_INTERFACE(chr->getAIInterface());
	if (!aichrInter)
	{
		return true;
	}

	ChannelPtr chnPtr = aichrInter->GetChannelPtr();

	if (chnPtr.isNull())
	{
		return true;
	}

	uint8 ui8Operate = WIK_NULL;
	uint8 ui8OperateCard = 0;
	uint8 ui8CardNum = 0;
	std::vector<uint8 > vecCards;
	vecCards.clear();
	packet >> ui8Operate >> ui8OperateCard;// >> ui8CardNum;
	for (uint8 i = 0; i < ui8CardNum; i++)
	{
		uint8 temp;
		packet >> temp;
		vecCards.push_back(temp);
	}


	AIMaJiangChannelBaseInterface * aichanInter = TO_MJCHANNEL_INTERFACE(chnPtr->getAIInterface());
	if (aichanInter)
	{
		aichanInter->UserOperateCard(chr, ui8OperateCard, ui8Operate, vecCards);
	}
	return true;
}

//CMSS_MJ_PlayerStatus = 0x250F,//玩家状态
bool  script_world_process_MJ_ChangeStatus(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr chr = socket->getCurrentCharacter();
	if (chr.isNull())
	{
		return true;
	}

	AICreatureBaseInterface* aichrInter = TO_CREATURE_BASE_INTERFACE(chr->getAIInterface());
	if (!aichrInter)
	{
		return true;
	}

	ChannelPtr chnPtr = aichrInter->GetChannelPtr();

	if (chnPtr.isNull())
	{
		return true;
	}

	uint8 ui8Stataus = 0;
	packet >> ui8Stataus;



	AIMaJiangChannelBaseInterface* aichanInter = TO_MJCHANNEL_INTERFACE(chnPtr->getAIInterface());

	if (!aichanInter)
	{
		return true;
	}
	aichanInter->SetPlayerStatus(chr, ui8Stataus);

	return true;
}

//CMSS_MJ_PlayerXuanQue = 0x2512,	//玩家选缺
bool  script_world_process_MJ_PlayerXuanQue(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr chr = socket->getCurrentCharacter();
	if (chr.isNull())
	{
		return true;
	}

	AICreatureBaseInterface* aichrInter = TO_CREATURE_BASE_INTERFACE(chr->getAIInterface());
	if (!aichrInter)
	{
		return true;
	}

	ChannelPtr chnPtr = aichrInter->GetChannelPtr();

	if (chnPtr.isNull())
	{
		return true;
	}

	uint8 ui8QueHuaSe = 0;
	packet >> ui8QueHuaSe;



	AIMaJiangChannelBaseInterface* aichanInter = TO_MJCHANNEL_INTERFACE(chnPtr->getAIInterface());

	if (!aichanInter)
	{
		return true;
	}
	aichanInter->PlayerChooseQue(chr, ui8QueHuaSe);

	return true;
}

//CMSS_MJ_PlayerSwapCard = 0x2515,		//玩家换牌
bool  script_world_process_MJ_SwapCard(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr chr = socket->getCurrentCharacter();
	if (chr.isNull())
	{
		return true;
	}

	AICreatureBaseInterface* aichrInter = TO_CREATURE_BASE_INTERFACE(chr->getAIInterface());
	if (!aichrInter)
	{
		return true;
	}

	ChannelPtr chnPtr = aichrInter->GetChannelPtr();

	if (chnPtr.isNull())
	{
		return true;
	}

	uint8 ui8CardNum = 0;
	std::vector<uint16 >  vecCards;
	packet >> ui8CardNum;
	for (uint8 i = 0; i < ui8CardNum; i++)
	{
		uint8 temp;
		packet >> temp;
		vecCards.push_back(temp);
	}



	AIMaJiangChannelBaseInterface* aichanInter = TO_MJCHANNEL_INTERFACE(chnPtr->getAIInterface());

	if (!aichanInter)
	{
		return true;
	}
	aichanInter->PlayerChooseSwapCards(chr, vecCards);

	return true;
}

//CMSS_MJ_BankerChooseSwapPos = 0x2520,			//玩家选择换牌位置
bool  script_world_process_MJ_SwapCardPos(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr chr = socket->getCurrentCharacter();
	if (chr.isNull())
	{
		return true;
	}

	AICreatureBaseInterface* aichrInter = TO_CREATURE_BASE_INTERFACE(chr->getAIInterface());
	if (!aichrInter)
	{
		return true;
	}

	ChannelPtr chnPtr = aichrInter->GetChannelPtr();

	if (chnPtr.isNull())
	{
		return true;
	}

	uint8 ui8SwapCardsPosType = 0;

	packet >> ui8SwapCardsPosType;
	
	AIMaJiangChannelBaseInterface* aichanInter = TO_MJCHANNEL_INTERFACE(chnPtr->getAIInterface());

	if (!aichanInter)
	{
		return true;
	}
	aichanInter->PlayerChooseSwapCardsType(chr, ui8SwapCardsPosType);

	return true;
}
